package com.rjwh.dingdong.client.network;

import com.rjwh.dingdong.client.constant.LocalConstant;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonHTTP {

    public static String getobject(String actionName, SoapObject request) {
        String result = "";
        MyAndroidHttpTransport androidHttpTransport = null;
        try {
            androidHttpTransport = new MyAndroidHttpTransport(LocalConstant.SERVER_URL, NetConnection.HTTP_CONNECT_TIME_OUT);
            androidHttpTransport.debug = true;
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
            envelope.bodyOut = request;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            try {
                androidHttpTransport.call(LocalConstant.NAMESPACE + actionName, envelope);
                SoapObject object = (SoapObject) envelope.bodyIn;
                result = object.getProperty(0).toString();
            } catch (Exception e) {

            }

            if (envelope.bodyIn instanceof SoapFault) {
                String str = ((SoapFault) envelope.bodyIn).faultstring;
                str = dealDoubleQuotes(str);
                result = str;
            } else {
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String dealDoubleQuotes(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\"");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");

        }
        return dest;
    }
}
